package Controller

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"web_back_go/DAO/gorm"
	"web_back_go/DAO/gorm/GormModel"
	"web_back_go/Response"
)

func GetAreaListByPid(c *gin.Context) {
	pidStr := c.Query("pid")
	pid, err := strconv.ParseInt(pidStr, 10, 64)
	if err != nil {
		c.JSON(200, Response.ResponseBody{
			Code:    "A00001",
			Msg:     "参数错误",
			Data:    nil,
			Version: "1.0.0",
			Fail:    true,
		})
		return
	}
	var areaList []GormModel.TzArea
	db := gorm.Db.Begin()
	//延迟回滚或提交事务
	defer func() {
		if err != nil {
			c.JSON(200, Response.ResponseBody{
				Code:    "A00001",
				Data:    nil,
				Success: true,
				Msg:     "服务器异常",
			})
			return
		}
	}()

	err = db.Where("parent_id = ?", pid).Find(&areaList).Error

	c.JSON(200, Response.ResponseBody{
		Code:    "00000",
		Data:    areaList,
		Success: true,
		Msg:     nil,
		Version: "1.0.0",
	})
}
